<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\StuffTuiModel;
use app\index\model\UserModel;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Printup extends Common
{

    public function stuff_ru(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['ids']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择数据');
        }

        $userChanid = (new UserService())->get_user_chanid();
        $data['chanid_name'] = DeptModel::get_name_by_pk($userChanid);
        $data['userid_name'] = $this->userinfo['realname'];

        $list = [];
        foreach ($ids as $ruid){
            $ruinfo = StuffRuModel::where(['id'=>$ruid])->find();
            $ruinfo['rudtzh'] = date("Y年m月d日", strtotime($ruinfo['rudt']));
            $ruinfo['enginid_name'] = EnginModel::where(['id'=>$ruinfo['enginid']])->value('name');
            $mingxi = StuffRuListModel::where(['ruid'=>$ruinfo['id']])->select()->toArray();
            $mingxi[] = [
                'name'=>'',
                'unit'=>'',
                'price'=>'',
                'num'=>'',
                'money'=>$ruinfo['money'],
                'explain'=>'',
            ];
            $ruinfo['mingxi'] = $mingxi;
            $list[] = $ruinfo;
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
   }
    public function stuff_danchu(Request $request){
        $param = $request->post();

        $numbers = array_filter(explode(',',$param['numbers']));
        if(count($numbers) == 0){
            return outputJson(4000,'请选择数据');
        }

        $userChanid = (new UserService())->get_user_chanid();
        $data['chanid_name'] = DeptModel::get_name_by_pk($userChanid);
        $data['userid_name'] = $this->userinfo['realname'];

        $list = [];
        foreach ($numbers as $number){
            $chuinfo = [];
            $mingxi = StuffChuModel::where(['number'=>$number])->select()->toArray();

            $all_noratefee = 0;
            $all_ratefee = 0;
            foreach ($mingxi as &$vvv){
                $ruinfo = StuffRuListModel::where(['id'=>$vvv['ruid']])->find();
                $vvv['name'] = $ruinfo['name'];
                $vvv['unit'] = $ruinfo['unit'];
                $vvv['price'] = $ruinfo['price'];
                $vvv['gonger'] = StuffRuModel::where(['id'=>$ruinfo['ruid']])->value('gonger');

                $vvv['rate'] = 0;
                $zhuinfo = StuffRuModel::where(['id'=>$ruinfo['ruid']])->find();
                if($zhuinfo['ishe'] == 1){
                    $vvv['rate'] = ContractCaiModel::where(['id'=>$zhuinfo['caiid']])->value('rate');
                    if(!$vvv['rate']){
                        $vvv['rate'] = 0;
                    }
                }
                if($zhuinfo['ishe'] == 2){
                    $vvv['rate'] = StuffCaiModel::where(['id'=>$zhuinfo['danid']])->value('rate');
                    if(!$vvv['rate']){
                        $vvv['rate'] = 0;
                    }
                }

                $vvv['ratefee'] = $vvv['price'] * $vvv['num'];
                $vvv['noratefee'] = $vvv['price'] / (1 + $vvv['rate']/100) * $vvv['num'];
                $all_noratefee += $vvv['noratefee'];
                $all_ratefee += $vvv['ratefee'];

                $vvv['ratefee'] = round($vvv['ratefee'], 2);
                $vvv['noratefee'] = round($vvv['noratefee'], 2);

            }

            $all_noratefee = round($all_noratefee, 2);
            $all_ratefee = round($all_ratefee, 2);

            $mingxi[] = [
                'name'=>'合计',
                'noratefee'=>$all_noratefee,
                'ratefee'=>$all_ratefee,
                'unit'=>'',
                'gonger'=>'',
                'num'=>'',
                'price'=>'',
                'rate'=>'',
                'explain'=>'',
            ];


            $chuinfo['mingxi'] = $mingxi;
            $chuinfo['enginid_name'] = EnginModel::where(['id'=>$mingxi[0]['enginid']])->value('name');
            $chuinfo['chudt'] = $mingxi[0]['chudt'];
            $chuinfo['number'] = $mingxi[0]['number'];
            $chuinfo['baner'] = $mingxi[0]['baner'];
            $list[] = $chuinfo;
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
   }
    public function stuff_dantui(Request $request){
        $param = $request->post();

        $numbers = array_filter(explode(',',$param['numbers']));
        if(count($numbers) == 0){
            return outputJson(4000,'请选择数据');
        }

        $userChanid = (new UserService())->get_user_chanid();
        $data['chanid_name'] = DeptModel::get_name_by_pk($userChanid);
        $data['userid_name'] = $this->userinfo['realname'];

        $list = [];
        foreach ($numbers as $number){
            $tuiinfo = [];
            $mingxi = StuffTuiModel::where(['number'=>$number])->select()->toArray();

            foreach ($mingxi as &$vvv){
                $ruinfo = StuffRuListModel::where(['id'=>$vvv['ruid']])->find();
                $vvv['name'] = $ruinfo['name'];
                $vvv['unit'] = $ruinfo['unit'];
                $vvv['gonger'] = StuffRuModel::where(['id'=>$ruinfo['ruid']])->value('gonger');
            }
            $tuiinfo['mingxi'] = $mingxi;
            $tuiinfo['enginid_name'] = EnginModel::where(['id'=>$mingxi[0]['enginid']])->value('name');
            $tuiinfo['tuidt'] = $mingxi[0]['tuidt'];
            $tuiinfo['number'] = $mingxi[0]['number'];
            $tuiinfo['baner'] = $mingxi[0]['baner'];
            $list[] = $tuiinfo;
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
   }



}

















